# 带着二哈去旅行丶:ZZQ
# 生成时间：2021/8/16 17:01
print('10-11')
import json
filename='number.json'
'''
number=input('请输入一个喜欢的数：')
with open(filename,'w')as wfile:
    json.dump(number,wfile)

with open(filename)as rfile:
    number=json.load(rfile)
    print(f'I know your favorite number! It‘s {number}')
'''
print('10-12')
'''
try:
    with open(filename)as rfile:
        number=json.load(rfile)
except:
    number = input('请输入一个喜欢的数：')
    with open(filename, 'w')as wfile:
        json.dump(number, wfile)
    with open(filename)as rfile:
        number = json.load(rfile)
else:
    print(f'I know your favorite number! It‘s {number}')
'''
print('10-13')
'''
def get_stored_username():
    filename='username.json'
    try:
        with open(filename)as rf:
            username=json.load(rf)
    except FileNotFoundError:
        return None
    else:
        return username
def get_new_username():
    username=input('what is your name?')
    filename='username.json'
    with open(filename,'w')as wf:
        json.dump(username,wf)
    return username
def greet_user():
    username=get_stored_username()
    if username:
        print(f'Welcome back,{username}!')
    else:
        username=get_new_username()
        print(f"We'll remember you when you conme back,{username}")

if __name__ == '__main__':
    greet_user()
'''
def get_stored_username():  # 判断文件是否存在
    filename='username.json'
    try:
        with open(filename)as rf:   # 默认读取 只读打开
            username=json.load(rf)
    except FileNotFoundError:       # 文件不存在
        return None
    else:
        return username

def get_new_username():
    username=input('what is your name?')
    filename='username.json'
    with open(filename,'w')as wf:       # 创建文件并写入
        json.dump(username,wf)
    return username

if __name__ == '__main__':
    username=get_stored_username()     # 判断是否文件存在
    if username:
        a=input(f'请问用户名是{username}吗？（y/n）：')
        if a=='y' or a=='Y':
            print(f'welcome come back!{username}')
        else:
            print('请重新输入用户名！')
            username = get_new_username()
    else:
        print('暂未保存用户，请输入用户名！')
        username = get_new_username()
        print(f"We'll remember you when you conme back,{username}")



